{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 对话机器人实例，来之langchain教程 ###"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4d016bb6afe9957f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain_ollama import ChatOllama\n",
    "chat = ChatOllama(base_url=\"10.12.8.21:11434\", model=\"qwen2.5:14b\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:07:18.735881Z",
     "start_time": "2024-10-15T07:07:17.427324Z"
    }
   },
   "id": "4f9171bb719a0536",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "AIMessage(content=\"Hello Bob! It's nice to meet you. How can I assist you today?\", additional_kwargs={}, response_metadata={'model': 'qwen2.5:14b', 'created_at': '2024-10-15T07:07:27.882724562Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 5736516994, 'load_duration': 5141115694, 'prompt_eval_count': 34, 'prompt_eval_duration': 161614000, 'eval_count': 18, 'eval_duration': 380897000}, id='run-c42ce17e-f3cf-4129-b74e-71432b010d5d-0', usage_metadata={'input_tokens': 34, 'output_tokens': 18, 'total_tokens': 52})"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "chat.invoke([HumanMessage(content=\"Hi! I'm Bob\")])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:07:27.820527Z",
     "start_time": "2024-10-15T07:07:22.055133Z"
    }
   },
   "id": "ce17a6bd503fc277",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "AIMessage(content=\"Your name is Bob, as you've just introduced yourself to me. Is there anything specific you would like help with, Bob?\", additional_kwargs={}, response_metadata={'model': 'qwen2.5:14b', 'created_at': '2024-10-15T07:08:45.45489508Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 958090988, 'load_duration': 826053, 'prompt_eval_count': 21, 'prompt_eval_duration': 230935000, 'eval_count': 27, 'eval_duration': 584008000}, id='run-654ed025-101c-40d3-bd92-f1686dae6b54-0', usage_metadata={'input_tokens': 21, 'output_tokens': 27, 'total_tokens': 48})"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import AIMessage\n",
    "\n",
    "chat.invoke(\n",
    "    [\n",
    "        HumanMessage(content=\"Hi! I'm Bob\"),\n",
    "        AIMessage(content=\"Hello Bob! How can I assist you today?\"),\n",
    "        HumanMessage(content=\"What's my name?\"),\n",
    "    ]\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:08:45.383965Z",
     "start_time": "2024-10-15T07:08:44.413434Z"
    }
   },
   "id": "5ce77dc675273111",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain_core.chat_history import (\n",
    "    BaseChatMessageHistory,\n",
    "    InMemoryChatMessageHistory,\n",
    ")\n",
    "from langchain_core.runnables.history import RunnableWithMessageHistory\n",
    "\n",
    "store = {}\n",
    "\n",
    "\n",
    "def get_session_history(session_id: str) -> BaseChatMessageHistory:\n",
    "    if session_id not in store:\n",
    "        store[session_id] = InMemoryChatMessageHistory()\n",
    "    return store[session_id]\n",
    "\n",
    "\n",
    "with_message_history = RunnableWithMessageHistory(chat, get_session_history)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:33:51.013713Z",
     "start_time": "2024-10-15T07:33:50.994714Z"
    }
   },
   "id": "a78ff77ab45d036c",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "config = {\"configurable\": {\"session_id\": \"abc2\"}}"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:33:53.600426Z",
     "start_time": "2024-10-15T07:33:53.589426Z"
    }
   },
   "id": "3a94fc5774ecfb7",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "\"Hello Bob! It's nice to meet you. How can I assist you today?\""
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"Hi! I'm Bob\")],\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:34:39.467599Z",
     "start_time": "2024-10-15T07:34:33.519557Z"
    }
   },
   "id": "30f19b48d7f53986",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'Your name is Bob, as you mentioned earlier. Is there anything specific you would like to ask or discuss?'"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"What's my name?\")],\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:35:19.285077Z",
     "start_time": "2024-10-15T07:35:18.358460Z"
    }
   },
   "id": "f105597637592c17",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "\"As an artificial intelligence, I don't have information about your personal identity unless you provide it to me. So, you haven't told me your name yet! What would you like me to call you?\""
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config = {\"configurable\": {\"session_id\": \"abc3\"}}\n",
    "\n",
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"What's my name?\")],\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:36:08.675352Z",
     "start_time": "2024-10-15T07:36:07.342168Z"
    }
   },
   "id": "b79094a8ee6857b8",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'Your name is Bob. If you have any questions or need help with something else, feel free to let me know!'"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config = {\"configurable\": {\"session_id\": \"abc2\"}}\n",
    "\n",
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"What's my name?\")],\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:36:36.722460Z",
     "start_time": "2024-10-15T07:36:35.604253Z"
    }
   },
   "id": "41f1b8e4f2835e5d",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"You are a helpful assistant. Answer all questions to the best of your ability.\",\n",
    "        ),\n",
    "        MessagesPlaceholder(variable_name=\"messages\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "chain = prompt | chat"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:38:03.815639Z",
     "start_time": "2024-10-15T07:38:03.809639Z"
    }
   },
   "id": "2d4b419b62609df7",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "\"Hello Bob! It's nice to meet you. How can I assist you today?\""
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = chain.invoke({\"messages\": [HumanMessage(content=\"hi! I'm bob\")]})\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:38:31.745156Z",
     "start_time": "2024-10-15T07:38:30.948404Z"
    }
   },
   "id": "7790081c91d734f1",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"You are a helpful assistant. Answer all questions to the best of your ability in {language}.\",\n",
    "        ),\n",
    "        MessagesPlaceholder(variable_name=\"messages\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "chain = prompt | chat"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:39:54.497484Z",
     "start_time": "2024-10-15T07:39:54.484484Z"
    }
   },
   "id": "370adcaccdab8134",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'¡Hola Bob! Es un gusto conocerte. ¿Cómo estás hoy? ¿En qué puedo ayudarte?'"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = chain.invoke(\n",
    "    {\"messages\": [HumanMessage(content=\"hi! I'm bob\")], \"language\": \"Spanish\"}\n",
    ")\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:40:09.453277Z",
     "start_time": "2024-10-15T07:40:08.523614Z"
    }
   },
   "id": "c0c94c2afdcdaa53",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'你好，Todd！有什么我可以帮忙的吗？'"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with_message_history = RunnableWithMessageHistory(\n",
    "    chain,\n",
    "    get_session_history,\n",
    "    input_messages_key=\"messages\",\n",
    ")\n",
    "config = {\"configurable\": {\"session_id\": \"abc11\"}}\n",
    "response = with_message_history.invoke(\n",
    "    {\"messages\": [HumanMessage(content=\"hi! I'm todd\")], \"language\": \"Chinese\"},\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:45:17.157459Z",
     "start_time": "2024-10-15T07:45:16.353566Z"
    }
   },
   "id": "48596aa3edee5462",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'你的名字是Todd。你需要帮助其他的事情吗？'"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    {\"messages\": [HumanMessage(content=\"whats my name?\")], \"language\": \"Chinese\"},\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T07:45:24.141778Z",
     "start_time": "2024-10-15T07:45:23.312306Z"
    }
   },
   "id": "8cfac0e88a86d1f1",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "Can't load tokenizer for 'gpt2'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'gpt2' is the correct path to a directory containing all relevant files for a GPT2TokenizerFast tokenizer.",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mOSError\u001B[0m                                   Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[21], line 27\u001B[0m\n\u001B[0;32m      4\u001B[0m trimmer \u001B[38;5;241m=\u001B[39m trim_messages(\n\u001B[0;32m      5\u001B[0m     max_tokens\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m65\u001B[39m,\n\u001B[0;32m      6\u001B[0m     strategy\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mlast\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m     10\u001B[0m     start_on\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mhuman\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     11\u001B[0m )\n\u001B[0;32m     13\u001B[0m messages \u001B[38;5;241m=\u001B[39m [\n\u001B[0;32m     14\u001B[0m     SystemMessage(content\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124myou\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mre a good assistant\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[0;32m     15\u001B[0m     HumanMessage(content\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mhi! I\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mm bob\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m     24\u001B[0m     AIMessage(content\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124myes!\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[0;32m     25\u001B[0m ]\n\u001B[1;32m---> 27\u001B[0m \u001B[43mtrimmer\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43minvoke\u001B[49m\u001B[43m(\u001B[49m\u001B[43mmessages\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\runnables\\base.py:4713\u001B[0m, in \u001B[0;36mRunnableLambda.invoke\u001B[1;34m(self, input, config, **kwargs)\u001B[0m\n\u001B[0;32m   4699\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Invoke this Runnable synchronously.\u001B[39;00m\n\u001B[0;32m   4700\u001B[0m \n\u001B[0;32m   4701\u001B[0m \u001B[38;5;124;03mArgs:\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   4710\u001B[0m \u001B[38;5;124;03m    TypeError: If the Runnable is a coroutine function.\u001B[39;00m\n\u001B[0;32m   4711\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m   4712\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mhasattr\u001B[39m(\u001B[38;5;28mself\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mfunc\u001B[39m\u001B[38;5;124m\"\u001B[39m):\n\u001B[1;32m-> 4713\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_call_with_config(\n\u001B[0;32m   4714\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_invoke,\n\u001B[0;32m   4715\u001B[0m         \u001B[38;5;28minput\u001B[39m,\n\u001B[0;32m   4716\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_config(config, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mfunc),\n\u001B[0;32m   4717\u001B[0m         \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs,\n\u001B[0;32m   4718\u001B[0m     )\n\u001B[0;32m   4719\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m   4720\u001B[0m     msg \u001B[38;5;241m=\u001B[39m (\n\u001B[0;32m   4721\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mCannot invoke a coroutine function synchronously.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   4722\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mUse `ainvoke` instead.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   4723\u001B[0m     )\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\runnables\\base.py:1927\u001B[0m, in \u001B[0;36mRunnable._call_with_config\u001B[1;34m(self, func, input, config, run_type, serialized, **kwargs)\u001B[0m\n\u001B[0;32m   1923\u001B[0m     context \u001B[38;5;241m=\u001B[39m copy_context()\n\u001B[0;32m   1924\u001B[0m     context\u001B[38;5;241m.\u001B[39mrun(_set_config_context, child_config)\n\u001B[0;32m   1925\u001B[0m     output \u001B[38;5;241m=\u001B[39m cast(\n\u001B[0;32m   1926\u001B[0m         Output,\n\u001B[1;32m-> 1927\u001B[0m         context\u001B[38;5;241m.\u001B[39mrun(\n\u001B[0;32m   1928\u001B[0m             call_func_with_variable_args,  \u001B[38;5;66;03m# type: ignore[arg-type]\u001B[39;00m\n\u001B[0;32m   1929\u001B[0m             func,  \u001B[38;5;66;03m# type: ignore[arg-type]\u001B[39;00m\n\u001B[0;32m   1930\u001B[0m             \u001B[38;5;28minput\u001B[39m,  \u001B[38;5;66;03m# type: ignore[arg-type]\u001B[39;00m\n\u001B[0;32m   1931\u001B[0m             config,\n\u001B[0;32m   1932\u001B[0m             run_manager,\n\u001B[0;32m   1933\u001B[0m             \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs,\n\u001B[0;32m   1934\u001B[0m         ),\n\u001B[0;32m   1935\u001B[0m     )\n\u001B[0;32m   1936\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[0;32m   1937\u001B[0m     run_manager\u001B[38;5;241m.\u001B[39mon_chain_error(e)\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\runnables\\config.py:396\u001B[0m, in \u001B[0;36mcall_func_with_variable_args\u001B[1;34m(func, input, config, run_manager, **kwargs)\u001B[0m\n\u001B[0;32m    394\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m run_manager \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m accepts_run_manager(func):\n\u001B[0;32m    395\u001B[0m     kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrun_manager\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m run_manager\n\u001B[1;32m--> 396\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m func(\u001B[38;5;28minput\u001B[39m, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\runnables\\base.py:4567\u001B[0m, in \u001B[0;36mRunnableLambda._invoke\u001B[1;34m(self, input, run_manager, config, **kwargs)\u001B[0m\n\u001B[0;32m   4565\u001B[0m                 output \u001B[38;5;241m=\u001B[39m chunk\n\u001B[0;32m   4566\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m-> 4567\u001B[0m     output \u001B[38;5;241m=\u001B[39m call_func_with_variable_args(\n\u001B[0;32m   4568\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mfunc, \u001B[38;5;28minput\u001B[39m, config, run_manager, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs\n\u001B[0;32m   4569\u001B[0m     )\n\u001B[0;32m   4570\u001B[0m \u001B[38;5;66;03m# If the output is a Runnable, invoke it\u001B[39;00m\n\u001B[0;32m   4571\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(output, Runnable):\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\runnables\\config.py:396\u001B[0m, in \u001B[0;36mcall_func_with_variable_args\u001B[1;34m(func, input, config, run_manager, **kwargs)\u001B[0m\n\u001B[0;32m    394\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m run_manager \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m accepts_run_manager(func):\n\u001B[0;32m    395\u001B[0m     kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrun_manager\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m run_manager\n\u001B[1;32m--> 396\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m func(\u001B[38;5;28minput\u001B[39m, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\messages\\utils.py:856\u001B[0m, in \u001B[0;36mtrim_messages\u001B[1;34m(messages, max_tokens, token_counter, strategy, allow_partial, end_on, start_on, include_system, text_splitter)\u001B[0m\n\u001B[0;32m    847\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m _first_max_tokens(\n\u001B[0;32m    848\u001B[0m         messages,\n\u001B[0;32m    849\u001B[0m         max_tokens\u001B[38;5;241m=\u001B[39mmax_tokens,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    853\u001B[0m         end_on\u001B[38;5;241m=\u001B[39mend_on,\n\u001B[0;32m    854\u001B[0m     )\n\u001B[0;32m    855\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m strategy \u001B[38;5;241m==\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mlast\u001B[39m\u001B[38;5;124m\"\u001B[39m:\n\u001B[1;32m--> 856\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_last_max_tokens\u001B[49m\u001B[43m(\u001B[49m\n\u001B[0;32m    857\u001B[0m \u001B[43m        \u001B[49m\u001B[43mmessages\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    858\u001B[0m \u001B[43m        \u001B[49m\u001B[43mmax_tokens\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmax_tokens\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    859\u001B[0m \u001B[43m        \u001B[49m\u001B[43mtoken_counter\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mlist_token_counter\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    860\u001B[0m \u001B[43m        \u001B[49m\u001B[43mallow_partial\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mallow_partial\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    861\u001B[0m \u001B[43m        \u001B[49m\u001B[43minclude_system\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43minclude_system\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    862\u001B[0m \u001B[43m        \u001B[49m\u001B[43mstart_on\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mstart_on\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    863\u001B[0m \u001B[43m        \u001B[49m\u001B[43mend_on\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mend_on\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    864\u001B[0m \u001B[43m        \u001B[49m\u001B[43mtext_splitter\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtext_splitter_fn\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    865\u001B[0m \u001B[43m    \u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    866\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    867\u001B[0m     msg \u001B[38;5;241m=\u001B[39m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mUnrecognized \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mstrategy\u001B[38;5;132;01m=}\u001B[39;00m\u001B[38;5;124m. Supported strategies are \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mlast\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m and \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mfirst\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\messages\\utils.py:969\u001B[0m, in \u001B[0;36m_last_max_tokens\u001B[1;34m(messages, max_tokens, token_counter, text_splitter, allow_partial, include_system, start_on, end_on)\u001B[0m\n\u001B[0;32m    966\u001B[0m swapped_system \u001B[38;5;241m=\u001B[39m include_system \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(messages[\u001B[38;5;241m0\u001B[39m], SystemMessage)\n\u001B[0;32m    967\u001B[0m reversed_ \u001B[38;5;241m=\u001B[39m messages[:\u001B[38;5;241m1\u001B[39m] \u001B[38;5;241m+\u001B[39m messages[\u001B[38;5;241m1\u001B[39m:][::\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m] \u001B[38;5;28;01mif\u001B[39;00m swapped_system \u001B[38;5;28;01melse\u001B[39;00m messages[::\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m]\n\u001B[1;32m--> 969\u001B[0m reversed_ \u001B[38;5;241m=\u001B[39m \u001B[43m_first_max_tokens\u001B[49m\u001B[43m(\u001B[49m\n\u001B[0;32m    970\u001B[0m \u001B[43m    \u001B[49m\u001B[43mreversed_\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    971\u001B[0m \u001B[43m    \u001B[49m\u001B[43mmax_tokens\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmax_tokens\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    972\u001B[0m \u001B[43m    \u001B[49m\u001B[43mtoken_counter\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtoken_counter\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    973\u001B[0m \u001B[43m    \u001B[49m\u001B[43mtext_splitter\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtext_splitter\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    974\u001B[0m \u001B[43m    \u001B[49m\u001B[43mpartial_strategy\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mlast\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mif\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mallow_partial\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01melse\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mNone\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[0;32m    975\u001B[0m \u001B[43m    \u001B[49m\u001B[43mend_on\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mstart_on\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    976\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    977\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m swapped_system:\n\u001B[0;32m    978\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m reversed_[:\u001B[38;5;241m1\u001B[39m] \u001B[38;5;241m+\u001B[39m reversed_[\u001B[38;5;241m1\u001B[39m:][::\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m]\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\messages\\utils.py:885\u001B[0m, in \u001B[0;36m_first_max_tokens\u001B[1;34m(messages, max_tokens, token_counter, text_splitter, partial_strategy, end_on)\u001B[0m\n\u001B[0;32m    883\u001B[0m idx \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m0\u001B[39m\n\u001B[0;32m    884\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(\u001B[38;5;28mlen\u001B[39m(messages)):\n\u001B[1;32m--> 885\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[43mtoken_counter\u001B[49m\u001B[43m(\u001B[49m\u001B[43mmessages\u001B[49m\u001B[43m[\u001B[49m\u001B[43m:\u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43mi\u001B[49m\u001B[43m]\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mif\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mi\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01melse\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mmessages\u001B[49m\u001B[43m)\u001B[49m \u001B[38;5;241m<\u001B[39m\u001B[38;5;241m=\u001B[39m max_tokens:\n\u001B[0;32m    886\u001B[0m         idx \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mlen\u001B[39m(messages) \u001B[38;5;241m-\u001B[39m i\n\u001B[0;32m    887\u001B[0m         \u001B[38;5;28;01mbreak\u001B[39;00m\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\language_models\\base.py:378\u001B[0m, in \u001B[0;36mBaseLanguageModel.get_num_tokens_from_messages\u001B[1;34m(self, messages)\u001B[0m\n\u001B[0;32m    367\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_num_tokens_from_messages\u001B[39m(\u001B[38;5;28mself\u001B[39m, messages: \u001B[38;5;28mlist\u001B[39m[BaseMessage]) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28mint\u001B[39m:\n\u001B[0;32m    368\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"Get the number of tokens in the messages.\u001B[39;00m\n\u001B[0;32m    369\u001B[0m \n\u001B[0;32m    370\u001B[0m \u001B[38;5;124;03m    Useful for checking if an input fits in a model's context window.\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    376\u001B[0m \u001B[38;5;124;03m        The sum of the number of tokens across the messages.\u001B[39;00m\n\u001B[0;32m    377\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[1;32m--> 378\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28msum\u001B[39m([\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mget_num_tokens(get_buffer_string([m])) \u001B[38;5;28;01mfor\u001B[39;00m m \u001B[38;5;129;01min\u001B[39;00m messages])\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\language_models\\base.py:378\u001B[0m, in \u001B[0;36m<listcomp>\u001B[1;34m(.0)\u001B[0m\n\u001B[0;32m    367\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_num_tokens_from_messages\u001B[39m(\u001B[38;5;28mself\u001B[39m, messages: \u001B[38;5;28mlist\u001B[39m[BaseMessage]) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28mint\u001B[39m:\n\u001B[0;32m    368\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"Get the number of tokens in the messages.\u001B[39;00m\n\u001B[0;32m    369\u001B[0m \n\u001B[0;32m    370\u001B[0m \u001B[38;5;124;03m    Useful for checking if an input fits in a model's context window.\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    376\u001B[0m \u001B[38;5;124;03m        The sum of the number of tokens across the messages.\u001B[39;00m\n\u001B[0;32m    377\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[1;32m--> 378\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28msum\u001B[39m([\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget_num_tokens\u001B[49m\u001B[43m(\u001B[49m\u001B[43mget_buffer_string\u001B[49m\u001B[43m(\u001B[49m\u001B[43m[\u001B[49m\u001B[43mm\u001B[49m\u001B[43m]\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m \u001B[38;5;28;01mfor\u001B[39;00m m \u001B[38;5;129;01min\u001B[39;00m messages])\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\language_models\\base.py:365\u001B[0m, in \u001B[0;36mBaseLanguageModel.get_num_tokens\u001B[1;34m(self, text)\u001B[0m\n\u001B[0;32m    354\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_num_tokens\u001B[39m(\u001B[38;5;28mself\u001B[39m, text: \u001B[38;5;28mstr\u001B[39m) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28mint\u001B[39m:\n\u001B[0;32m    355\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"Get the number of tokens present in the text.\u001B[39;00m\n\u001B[0;32m    356\u001B[0m \n\u001B[0;32m    357\u001B[0m \u001B[38;5;124;03m    Useful for checking if an input fits in a model's context window.\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    363\u001B[0m \u001B[38;5;124;03m        The integer number of tokens in the text.\u001B[39;00m\n\u001B[0;32m    364\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[1;32m--> 365\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget_token_ids\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtext\u001B[49m\u001B[43m)\u001B[49m)\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\language_models\\base.py:352\u001B[0m, in \u001B[0;36mBaseLanguageModel.get_token_ids\u001B[1;34m(self, text)\u001B[0m\n\u001B[0;32m    350\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcustom_get_token_ids(text)\n\u001B[0;32m    351\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m--> 352\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_get_token_ids_default_method\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtext\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\language_models\\base.py:76\u001B[0m, in \u001B[0;36m_get_token_ids_default_method\u001B[1;34m(text)\u001B[0m\n\u001B[0;32m     74\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Encode the text into token IDs.\"\"\"\u001B[39;00m\n\u001B[0;32m     75\u001B[0m \u001B[38;5;66;03m# get the cached tokenizer\u001B[39;00m\n\u001B[1;32m---> 76\u001B[0m tokenizer \u001B[38;5;241m=\u001B[39m \u001B[43mget_tokenizer\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m     78\u001B[0m \u001B[38;5;66;03m# tokenize the text using the GPT-2 tokenizer\u001B[39;00m\n\u001B[0;32m     79\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m tokenizer\u001B[38;5;241m.\u001B[39mencode(text)\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\langchain_core\\language_models\\base.py:70\u001B[0m, in \u001B[0;36mget_tokenizer\u001B[1;34m()\u001B[0m\n\u001B[0;32m     68\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m(msg) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01me\u001B[39;00m\n\u001B[0;32m     69\u001B[0m \u001B[38;5;66;03m# create a GPT-2 tokenizer instance\u001B[39;00m\n\u001B[1;32m---> 70\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mGPT2TokenizerFast\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfrom_pretrained\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mgpt2\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mD:\\ProgramData\\anaconda3\\envs\\pytorch\\lib\\site-packages\\transformers\\tokenization_utils_base.py:2255\u001B[0m, in \u001B[0;36mPreTrainedTokenizerBase.from_pretrained\u001B[1;34m(cls, pretrained_model_name_or_path, cache_dir, force_download, local_files_only, token, revision, trust_remote_code, *init_inputs, **kwargs)\u001B[0m\n\u001B[0;32m   2252\u001B[0m \u001B[38;5;66;03m# If one passes a GGUF file path to `gguf_file` there is no need for this check as the tokenizer will be\u001B[39;00m\n\u001B[0;32m   2253\u001B[0m \u001B[38;5;66;03m# loaded directly from the GGUF file.\u001B[39;00m\n\u001B[0;32m   2254\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mall\u001B[39m(full_file_name \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;28;01mfor\u001B[39;00m full_file_name \u001B[38;5;129;01min\u001B[39;00m resolved_vocab_files\u001B[38;5;241m.\u001B[39mvalues()) \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m gguf_file:\n\u001B[1;32m-> 2255\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mEnvironmentError\u001B[39;00m(\n\u001B[0;32m   2256\u001B[0m         \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mCan\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mt load tokenizer for \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mpretrained_model_name_or_path\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m. If you were trying to load it from \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2257\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mhttps://huggingface.co/models\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m, make sure you don\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mt have a local directory with the same name. \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2258\u001B[0m         \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mOtherwise, make sure \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mpretrained_model_name_or_path\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m is the correct path to a directory \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2259\u001B[0m         \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcontaining all relevant files for a \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39m\u001B[38;5;18m__name__\u001B[39m\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m tokenizer.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2260\u001B[0m     )\n\u001B[0;32m   2262\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m file_id, file_path \u001B[38;5;129;01min\u001B[39;00m vocab_files\u001B[38;5;241m.\u001B[39mitems():\n\u001B[0;32m   2263\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m file_id \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;129;01min\u001B[39;00m resolved_vocab_files:\n",
      "\u001B[1;31mOSError\u001B[0m: Can't load tokenizer for 'gpt2'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'gpt2' is the correct path to a directory containing all relevant files for a GPT2TokenizerFast tokenizer."
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'\n",
    "from langchain_core.messages import SystemMessage, trim_messages\n",
    "trimmer = trim_messages(\n",
    "    max_tokens=65,\n",
    "    strategy=\"last\",\n",
    "    token_counter=chat,\n",
    "    include_system=True,\n",
    "    allow_partial=False,\n",
    "    start_on=\"human\",\n",
    ")\n",
    "\n",
    "messages = [\n",
    "    SystemMessage(content=\"you're a good assistant\"),\n",
    "    HumanMessage(content=\"hi! I'm bob\"),\n",
    "    AIMessage(content=\"hi!\"),\n",
    "    HumanMessage(content=\"I like vanilla ice cream\"),\n",
    "    AIMessage(content=\"nice\"),\n",
    "    HumanMessage(content=\"whats 2 + 2\"),\n",
    "    AIMessage(content=\"4\"),\n",
    "    HumanMessage(content=\"thanks\"),\n",
    "    AIMessage(content=\"no problem!\"),\n",
    "    HumanMessage(content=\"having fun?\"),\n",
    "    AIMessage(content=\"yes!\"),\n",
    "]\n",
    "\n",
    "trimmer.invoke(messages)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-15T08:47:36.415579Z",
     "start_time": "2024-10-15T08:46:26.132873Z"
    }
   },
   "id": "f14c0f92fae08340",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "e190e0ff5a75c51b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
